package com.xtrip.adm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tcl.platform.database.query.SimpleQuery;
import com.xtrip.adm.model.Area;
import com.xtrip.adm.model.CBD;
import com.xtrip.adm.model.City;
import com.xtrip.adm.model.Province;
import com.xtrip.adm.service.AreaService;
import com.xtrip.adm.service.CBDService;
import com.xtrip.adm.service.CityService;
import com.xtrip.adm.service.ProvinceService;
import com.xtrip.adm.stat.data.TcHotelGeo;
import com.xtrip.base.BaseController;
import com.xtrip.common.BusinessConstant;
import com.xtrip.common.TcConstant;
import com.xtrip.net.request.TcRequest;
import com.xtrip.net.response.ResponseDataParser;
import com.xtrip.net.response.TcAreaRsp;
import com.xtrip.net.util.RequestSend;
import com.xtrip.util.CharConverter;
import com.xtrip.util.XtripLogger;
import com.xtrip.util.parse.xml.CtripParser;
import com.xtrip.util.parse.xml.XtripDocumentHelper;

@Controller("mgeoController")
@RequestMapping("mgeo")
public class MGeoController extends BaseController{

	@Resource(name = "cityService")
	private CityService	cityService;
	@Resource(name = "cbdService")
	private CBDService	cbdService;
	@Resource(name = "provinceService")
	private ProvinceService	provinceService;
	@Resource(name = "areaService")
	private AreaService	areaService;
	
	private XtripLogger log = getLogger(MGeoController.class);

	@RequestMapping("/index")
	public String index(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is mgeoController");
		return "/adm/index";
	}
	@RequestMapping("/addTcPCCBD")
	public void addTcPCCBD(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addTcPCCBD");
		
		TcHotelGeo thg = new TcHotelGeo();
		thg.parse("E:\\nexttrips\\17uu\\静态数据文件\\hotel-goe-cn.xml");
		List<Province> pList = thg.getpList();
		List<City> cList = thg.getcList();
		List<CBD> cbdList = thg.getCbdList();
		log.start("导入TC省份信息");
		for(Province p:pList){
			provinceService.insertWithId(p);
		}
		log.end("导入TC省份信息");
		int csuccCnt = 0;
		int cfailCnt = 0;
		int cbdsuccCnt = 0;
		int cbdfailCnt = 0;
		int ctot = cList.size();
		int cbdtot = cbdList.size();
		log.start("导入TC城市信息");
		for(City city:cList){
			boolean succ = cityService.insertWithId(city);
			if(succ)csuccCnt++;
			else {
				log.i("name:"+city.getName()+",tcid:"+city.getTcCid()+",provId:"+city.getProId()+",pinyin:"+city.getPinyin()+",index:"+city.getAbcd());
				cfailCnt++;
			}
		}
		log.i("TOTAL:"+ctot+"  SUCCESS:"+csuccCnt+"  FAIL:"+cfailCnt);
		log.end("导入TC城市信息");
		log.start("导入TC商圈信息");
		for(CBD cbd:cbdList){
			boolean succ = cbdService.insertWithId(cbd);
			if(succ)cbdsuccCnt++;
			else {
				log.i("name:"+cbd.getName()+",id:"+cbd.getId()+",tc_cbdid:"+cbd.getTcCBDId()+",city_id:"+cbd.getCityId());
				cbdfailCnt++;
			}
		}
		log.i("TOTAL:"+cbdtot+"  SUCCESS:"+cbdsuccCnt+"  FAIL:"+cbdfailCnt);
		log.end("导入TC商圈信息");
		JSONObject jo = new JSONObject();
		jo.put("csuccCnt", csuccCnt);
		jo.put("cfailCnt", cfailCnt);
		jo.put("ctot", ctot);
		jo.put("cbdtot", cbdtot);
		out(response, jo);
	}
	/*
	 * api 录入同程区域信息
	 * 依赖于城市列表中同程城市id  tcCId 字段 循环请求入库
	 */
	@RequestMapping("/addTcArea")
	public void addTcArea(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addTcArea");
		SimpleQuery sq = new SimpleQuery(City.class);
		List<City> clist = sq.list();
		log.start("导入TC行政区信息");
		for(City c : clist){
			String tcCityId = c.getTcCid();
			String cityId = c.getId();
			Map<String,String> params = new HashMap<String,String>();
			params.put("cityId", tcCityId);
			String data = TcRequest.generateXml(TcConstant.METHOD_AREA, params);
//			System.out.println(data);
			String resp = new RequestSend(TcConstant.REQUEST_BASE_URL, BusinessConstant.TYPE_HOTEL, BusinessConstant.PROVIDER_HOTEL_17U, data).post();
//			System.out.println(resp);
			TcAreaRsp crsp = new TcAreaRsp();
			crsp.parse(resp);
			List<Area> list = crsp.getList();
//			System.out.println(list.size());
//			System.out.println(crsp.getTotalCount());
			int tot = list.size();
			int succCnt = 0;
			int failCnt = 0;
			for(Area ar : list){
				ar.setCityId(cityId);
				boolean succ = areaService.insertWithId(ar);
				if(succ)succCnt++;
				else {
					log.i("tc_city_id"+"Id:"+ar.getId()+",name:"+ar.getName()+",index:"+ar.getIndex()+",eName:"+ar.geteName());
					failCnt++;
				}
			}
			log.i("CITY:"+c.getName()+" CITY_ID:"+c.getId()+"  TOTAL:"+tot+"  SUCCESS:"+succCnt+"  FAIL:"+failCnt);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		log.end("导入TC行政区信息");
		JSONObject jo = new JSONObject();
//		jo.put("succCnt", succCnt);
//		jo.put("failCnt", failCnt);
		out(response, jo);
	}
	
	@RequestMapping("/addZhunaCity")
	public void addZhunaCity(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCity");
		Map<String,String> prov = cityService.getProvinceMap();
		SimpleQuery sq = new SimpleQuery(City.class);
		List<City> l = sq.list();
		Map<String,String> params = new HashMap<String,String>();
		params.put("method", "city");
		params.put("agent_id", "3279544");
		params.put("agent_md", "e442f724b7c8c5d8");
		String result = new RequestSend(BusinessConstant.REQUEST_URL_ZHUNA,
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, params).post();
		List<City> list = (List<City>) ResponseDataParser.parse(
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, result);
		int succCnt = 0;
		int failCnt = 0;
		int k = 0;
		for(City tmp:list){
			System.out.println("正在导入第"+k+"条数据");
			boolean succ = false;
			for(City city:l){
				String cname = tmp.getName();
				String cid = tmp.getZhunaCid();
				int i = -1;
				if((i=cname.indexOf("（"))!=-1){
					cname = cname.substring(0, i);
				}
				if(cname.length()>2 && cname.substring(cname.length()-1).equals("市")){
					cname = cname.substring(0, cname.length()-1);
				}
				if(cname.length()>2 && cname.substring(cname.length()-1).equals("县")){
					cname = cname.substring(0, cname.length()-1);
				}
				if(cname.equals(city.getName())){
					city.setZhunaCid(cid);
					city.setZhunaHCnt(tmp.getZhunaHCnt());
					succ = cityService.update(city);
					succCnt++;
				}
			}
			if(!succ){
				System.out.println("第"+k+"条数据，导入失败！");
				System.out.println("name:"+tmp.getName()+",zhunaCid:"+tmp.getZhunaCid()+",provId:"+prov.get(tmp.getProId())+",zhunacnt:"+tmp.getZhunaHCnt());
				failCnt ++;
			}else{
				System.out.println("第"+k+"条数据，导入成功！");
			}
			k++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		out(response, jo);
	}
	@RequestMapping("/updateZhunaHotel")
	public void updateZhunaHotel(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCity");
		
		Map<String,String> params = new HashMap<String,String>();
		params.put("method", "city");
		params.put("agent_id", "3279544");
		params.put("agent_md", "e442f724b7c8c5d8");
		String result = new RequestSend(BusinessConstant.REQUEST_URL_ZHUNA,
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, params).post();
		List<City> list = (List<City>) ResponseDataParser.parse(
				BusinessConstant.TYPE_HOTEL,
				BusinessConstant.PROVIDER_HOTEL_ZHUNA, result);
		int succCnt = 0;
		int failCnt = 0;
		for(City city:list){
			boolean succ = cityService.insertCity(city);
			if(succ)succCnt++;
			else failCnt++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		out(response, jo);
	}
	@RequestMapping("/addCtripCity")
	public void addCtripCity(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		System.out.println("Hello this is addCtripCity");
		
		Document doc = null;
		try {
			doc = XtripDocumentHelper.readFile("E:\\nexttrips\\ctrip\\API2.0相关静态文件\\国内城市.xml");
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String itemName = "CityDetail";
		List<City> list = CtripParser.parseCtripCity(doc, itemName);
		int succCnt = 0;
		int failCnt = 0;
		int tot = list.size();
		for(City city:list){
			String name = city.getName();
			String pinyin = CharConverter.getPingYin(name);
			String suoxie = CharConverter.getPinYinHeadChar(name);
			String abcd = suoxie.substring(0, 1);
			city.setAbcd(abcd);
			city.setPinyin(pinyin);
			city.setSuoxie(suoxie);
			boolean succ = cityService.insertCity(city);
			if(succ)succCnt++;
			else failCnt++;
		}
		JSONObject jo = new JSONObject();
		jo.put("succCnt", succCnt);
		jo.put("failCnt", failCnt);
		jo.put("tot", tot);
		out(response, jo);
	}
}
